package com.qs.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

import java.util.concurrent.CountDownLatch;

public class MyWatcher implements Watcher {

    private CountDownLatch countDownLatch;

    public MyWatcher(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println("===>>state=" + watchedEvent.getState() + ",type=" + watchedEvent.getType());
        Event.EventType type = watchedEvent.getType();
        String path = watchedEvent.getPath();
        if (type == Event.EventType.NodeDeleted) {
            System.out.println("删除节点：" + path);
        } else if (type == Event.EventType.NodeCreated) {
            System.out.println("创建节点：" + path);
        } else if (type == Event.EventType.NodeDataChanged) {
            System.out.println("修改节点数据，节点：" + path);
        } else if (type == Event.EventType.NodeChildrenChanged) {
            System.out.println("子节点数据改变，节点：" + path);
        }

        countDownLatch.countDown();
    }
}
